//
//  HJNewFeatureViewController.swift
//  HJWeiBo
//
//  Created by 黄珏 on 15/10/31.
//  Copyright © 2015年 黄珏. All rights reserved.
//

import UIKit

class HJNewFeatureViewController: UICollectionViewController {
    
    private var viewLayout = UICollectionViewFlowLayout()
    init () {
        super.init(collectionViewLayout: viewLayout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
//        注册cell
        self.collectionView?.registerClass(HJCollectionViewCell.self, forCellWithReuseIdentifier: "CollectionCell")
        setupCollectionView()
    }
    /**
    设置collectionView属性
    */
    func setupCollectionView() {
        viewLayout.itemSize = UIScreen.mainScreen().bounds.size
        viewLayout.minimumInteritemSpacing = 0
        viewLayout.minimumLineSpacing = 0
        viewLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }
//    MARK: - 代理方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath) as! HJCollectionViewCell
        cell.imageIndex = indexPath.item
        return cell
    }
    // collectionView显示完毕cell
    // collectionView分页滚动完毕cell看不到的时候调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 正在显示的cell的indexPath
        let showIndexPath = collectionView.indexPathsForVisibleItems().first!
        print(showIndexPath)
        // 获取collectionView正在显示的cell
        let cell = collectionView.cellForItemAtIndexPath(showIndexPath) as! HJCollectionViewCell
        
        // 最后一页动画
        if showIndexPath.item == 3 {
            // 开始按钮动画
            cell.startButtonAnimation()
        }
    }
}

class HJCollectionViewCell: UICollectionViewCell {
//    startButton按钮点击事件
    func startButtonAction() {
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
    }
    
    func startButtonAnimation() {
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.6, delay: 0.1, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }){ (_) -> Void in
                
        }
    }
    
    var imageIndex: Int? {
        didSet{
            backgroundImage.image = UIImage(named: "new_feature_\(imageIndex! + 1)")
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func prepareUI() {
//        添加子空间
        contentView.addSubview(backgroundImage)
        contentView.addSubview(startButton)
        backgroundImage.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
//        添加约束
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImage, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
//        给按钮添加约束
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    lazy var backgroundImage = UIImageView()
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.addTarget(self, action: "startButtonAction", forControlEvents: UIControlEvents.TouchUpInside)
        button.sizeToFit()
        return button
    }()
}
